<?php
class Language extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model("Mlanguage");
	}
	
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_language'),base_url().'admincp/language');
		if($this->input->is_ajax_request()){
            $output = $this->Mlanguage->listLanguage();
            $this->output->set_output(json_encode($output));
        }else{
            $data['template']="admincp/language/view";
            $data['title_page']='<i class="icon20 i-dashboard"></i> '.trans('title_language');
			$this->load->view("admincp/template",$data);
        }
	}

	public function home(){
		$data['language'] = $this->registry->get('language');
		$this->load->view("language/home",$data);
	}

	public function add(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_language'),base_url().'admincp/language');
		$this->breadcrumb->append_crumb(trans('breadcrumb_language_add'),base_url().'admincp/language/add');
		$data['template']="language/add";
		$data['title_page']='<i class="icon20 i-plus-circle-2"></i> '.trans('title_language_add');
		if($this->input->post("ok") != ""){
			$this->form_validation->set_rules("lang_code","lang code","required|callback_check_code");
			$this->form_validation->set_rules("country","country","required");
			if($this->form_validation->run() == FALSE){ 
				$error = validation_errors();
				$error = explode("\n",$error);
				foreach($error as $err){
					if($err != ""){
           				$data['error'][] = $err;
					}
				}
			}else{
				$data_insert = array(
					'lang_code' => $this->input->post('lang_code'),
					'country' => $this->input->post('country')
				);
				$this->Mlanguage->insert($data_insert);
				$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
				redirect(base_url()."admincp/language");		
			}
		}
		$this->load->view("admincp/template",$data);
	}

	public function edit(){
		$id = $this->uri->segment(5);
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_language'),base_url().'admincp/language');
		$this->breadcrumb->append_crumb(trans('breadcrumb_language_edit'),base_url().'admincp/language/edit/id/'.$id);
		$data['template']="language/edit";
		$data['title_page']='<i class="icon20 i-dashboard"></i> '.trans('title_language_edit');
		$data['info'] = $this->Mlanguage->get_data($id);
		if($this->input->post("ok") != ""){
			$this->form_validation->set_rules("lang_code","lang code","required|callback_check_code");
			$this->form_validation->set_rules("country","country","required");
			if($this->form_validation->run() == FALSE){ 
				$error = validation_errors();
				$error = explode("\n",$error);
				foreach($error as $err){
					if($err != ""){
           				$data['error'][] = $err;
					}
				}
			}else{
				$data_update = array(
					'lang_code' => $this->input->post('lang_code'),
					'country' => $this->input->post('country')
				);
				$this->Mlanguage->update($data_update,$id);
				$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
				redirect(base_url()."admincp/language");		
			}
		}
		$this->load->view("admincp/template",$data);
	}

	public function delete(){
		$id = $this->uri->segment(5);
		$this->Mlanguage->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(base_url()."admincp/language");
	}
	
	public function check_code($code, $id){
		if($this->Mlanguage->check_code($code, $id) == FALSE){
			return FALSE;
		}else{
			return TRUE;
		}
	}

	public function changeLanguage(){
		$code = $this->input->post('code');
		$data_ses["current_language"] = array('code' => $code);
		$this->session->set_userdata($data_ses);
	}
}